package org.example.models;

import lombok.Getter;
import org.example.entity.Permissions;
import org.example.entity.Users;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Getter
public class LoginUser implements UserDetails {

    private Users user;


    private List<Permissions> permissions;

    public LoginUser(Users user, List<Permissions> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

//用户的权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
     return    permissions.stream().map(
                item->new SimpleGrantedAuthority(item.getPerm()))
                .collect((Collectors.toList()));

    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
